package com.example.demo.service.impl;

import com.example.demo.entity.EmpappEmp;
import com.example.demo.mapper.EmpappEmpMapper;
import com.example.demo.service.IEmpappEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2024-12-06
 */
@Service
public class EmpappEmpServiceImpl extends ServiceImpl<EmpappEmpMapper, EmpappEmp> implements IEmpappEmpService {

    private List<EmpappEmp> empappEmpList = new ArrayList<>();

    public EmpappEmpServiceImpl(){
        EmpappEmp empappEmp = new EmpappEmp();
        empappEmp.setName("唐僧");
        empappEmp.setAge(666);
        empappEmp.setSalary(new BigDecimal("60000"));
        empappEmp.setDep("运营部");
        empappEmp.setProvince("大唐");
        empappEmpList.add(empappEmp);
        EmpappEmp empappEmp2 = new EmpappEmp();
        empappEmp2.setName("孙悟空");
        empappEmp2.setAge(6666);
        empappEmp2.setSalary(new BigDecimal("60000"));
        empappEmp2.setDep("运营部");
        empappEmp2.setProvince("花果山");
        empappEmpList.add(empappEmp2);
        EmpappEmp empappEmp3 = new EmpappEmp();
        empappEmp3.setName("猪八戒");
        empappEmp3.setAge(9999);
        empappEmp3.setSalary(new BigDecimal("60000"));
        empappEmp3.setDep("运营部");
        empappEmp3.setProvince("高老庄");
        empappEmpList.add(empappEmp3);
        EmpappEmp empappEmp4 = new EmpappEmp();
        empappEmp4.setName("沙僧");
        empappEmp4.setAge(9999);
        empappEmp4.setSalary(new BigDecimal("60000"));
        empappEmp4.setDep("运营部");
        empappEmp4.setProvince("流沙河");
        empappEmpList.add(empappEmp4);
    }

    public void saveBatch(){
        List<List<EmpappEmp>> partition = partition(empappEmpList, 2);
        for (List<EmpappEmp> empappEmps : partition) {
            this.saveBatch(empappEmps);
        }
        empappEmpList.forEach(empappEmp -> {
            System.out.println("empappEmp = " + empappEmp);
        });
    }

    public static <T> List<List<T>> partition(List<T> list, int size) {
        if (list == null || list.isEmpty() || size <= 0) {
            return List.of();
        }
        return IntStream.range(0, (list.size() + size - 1) / size)
                .mapToObj(i -> list.subList(i * size, Math.min((i + 1) * size, list.size())))
                .collect(Collectors.toList());
    }

    public static <T> List<List<T>> partitionByCondition(List<T> list, Predicate<T> condition) {
        List<T> matchingList = list.stream().filter(condition).collect(Collectors.toList());
        List<T> nonMatchingList = list.stream().filter(element ->!condition.test(element)).collect(Collectors.toList());
        return List.of(matchingList, nonMatchingList);
    }

}
